#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=1e6+5;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<ll> sum(n);
    sum[0] = a[0];
    for (int i = 1; i < n; ++i) {
        sum[i] = sum[i - 1] + a[i];
    }
    string s;
    cin >> s;
    vector<int> left;
    for (int i = 0; i < n; ++i) {
        if (s[i] == 'L') {
            left.push_back(i);
        }
    }
    vector<int> right;
    for (int i = n - 1; i >= 0; --i) {
        if (s[i] == 'R') {
            right.push_back(i);
        }
    }
    ll ans = 0;
    int i = 0, j = 0;
    while (i < left.size() && j < right.size()) {
        if (left[i] < right[j]) {
            ans += sum[right[j]] - (left[i] > 0 ? sum[left[i] - 1] : 0);
            ++i;
            ++j;
        } else {
            ++j;
        }
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}